/* i_att_count ************* *************************************************
	This program produces Data Appendix Table B1
******************************************************************************/
set more off
local grades "K1"


foreach g of local grades {
matrix att_count = J(11, 7,-1)
local j = 1
forval y = 1997/2003 {
	local i = 1
	di "`y'"
	di "`i'"
	di "`j'"

	*Full sample
	use "$stata_data_assignment/initial_set.dta", clear
	keep if year == `y'
	keep if grade == "`g'"
	count
	matrix att_count[`i++',`j'] = r(N)

	*Get rid of disqualified applicants
	keep if !inlist(status,"I","R")
	count
	matrix att_count[`i++',`j'] = r(N)

	*Column 1: First round only
	keep if round=="1"
	count
	matrix att_count[`i++',`j'] = r(N)

	*Column 2: Valid Pref and priority
	use "$stata_data_assignment/`y'/pscores`g'.dta", clear
	unique studentno
	matrix att_count[`i++',`j'] = r(sum)

	*Column 3: Not duplicate or duplicate random
	use "$stata_data_analysis/nsc/nsc_ready_fin.dta", clear
	keep if yr_asgn == `y' & gr_asgn == "`g'"
	unique studentno
	matrix att_count[`i++',`j'] = r(sum)

	*Column 4: Has Primary ID/NSC
	use "$stata_data_analysis/nsc/nsc_ready_fin.dta", clear
	keep if yr_asgn == `y' & gr_asgn == "`g'" & find_prim_name == 1
	count
	matrix att_count[`i++',`j'] = r(N)

  keep if nsc_prim_name_link == 1
  count
  matrix att_count[`i++',`j'] = r(N)

	*Column 5: Has SASID
	use "$stata_data_analysis/full_samp.dta", clear
	keep if yr_asgn == `y' & in_sims == 1
	unique studentno
	matrix att_count[`i++',`j'] = r(sum)

  * Column 6: Among randomized applicants, drop duplicate random number or ID
  use "$stata_data_analysis/nsc/nsc_ready_fin.dta", clear
  keep if yr_asgn == `y' & gr_asgn == "`g'"
  unique studentno

  preserve
  keep if !inlist(pformany,0,1)
  count
  matrix att_count[`i++',`j'] = r(N)
  restore

  * Column 7: Among randomized applicants, has primary ID/NSC
  use "$stata_data_analysis/nsc/nsc_ready_fin.dta", clear
  keep if yr_asgn == `y' & gr_asgn == "`g'" & find_prim_name == 1
  keep if nsc_prim_name_link == 1

  preserve
  keep if !inlist(pformany,0,1)
  count
  matrix att_count[`i++',`j'] = r(N)
  restore

  * Column 8: Among randomized applicants, has SIMS
  use "$stata_data_analysis/full_samp.dta", clear
	keep if yr_asgn == `y' & in_sims == 1
	unique studentno

  preserve
  keep if !inlist(pformany,0,1)
  count
  matrix att_count[`i++',`j'] = r(N)
  restore

	local j = `j' + 1
	matrix list att_count


}

* Paste contents of table_b1.xlsx into cell B31 of sheet B1_att_count of the Excel deck
putexcel set "$results/raw_tabs/table_b1", replace
putexcel A1 = matrix(att_count)
}
